﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Collections.Specialized;
using CLibArtreve.security;
using CLibArtreve.dal;

namespace CLibArtreve.security
{
    public class ArtreveMembershipProvider : SqlMembershipProvider
    {
        //CONNECTIONSTRING
        private string _connectionString;
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }
        //PROPERTIES
        private UserDAL _adapter = null;
        protected UserDAL Adapter
        {
            get
            {
                if (_adapter == null)
                    _adapter = new UserDAL(ConnectionString);

                return _adapter;
            }
        }
        //INITIALIZE
        public override void Initialize(string name, NameValueCollection config)
        {
            ConnectionString = config["connectionString"];
            //REMOVE connectionString FROM config
            config.Remove("connectionString");
            base.Initialize(name, config);
        }
        //VALIDATE USER
        public override bool ValidateUser(string username, string password)
        {
            try
            {
                string hashedPassword = Crypto.ComputeHashMD5(password);
                bool userValidated = Adapter.ValidateUser(username, hashedPassword);
                return userValidated;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
    }
}
